package vn.game;

import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PlayGame {
	/** Called when the activity is first created. */
	public String tag = PlayGame.class.getSimpleName();
	HorizontalScrollView hScroll;
	EditText edit;
	EditText edtName;
	EditText edtKind;
	GridView gridView;
	Button btnSubmit;
	Button btnHint;
	Button btnFaceBook;

	// LinearLayout lnAcross;
	// LinearLayout lnDown;

	// int countNum = 0;
	// int countID = 0;
	String pathImg = null;
	int idEdit = 0;
	int wText = 35; // width of textbox

	public ArrayList<clsWordDetail> arrList;
	public ArrayList<clsWord> arrListGame;
	ListView lv1;
	LinearLayout lnMainLayout;
	LinearLayout lnGridItem;
	LinearLayout lnGrid;

	ImageView view2;

	// ListView lstAcross, lstDown;

	CrosswordHintArrayAdapter acrossAdapter;
	CrosswordHintArrayAdapter downAdapter;

	public ArrayList<Integer> acrossHint;
	public ArrayList<Integer> downHint;

	CrossWordDbAdapter mDbHelper;

	// Timer
	TextView lblDisplayTime;

	// cell
	int widthCell;

	// public EditViewAdapter EditAdapter = null;
	public Activity activity;
	public int idGame;

	public ArrayList<String> acrossHints, downHints;

	public PlayGame(Activity activity) {
		this.activity = activity;

		Init();

	}

	public void Init() {
		Log.i(tag, "init()");
		MainPlayActivity.row = -1;
		MainPlayActivity.column = -1;
		idGame = -1;
		arrList = new ArrayList<clsWordDetail>();
		acrossHints = new ArrayList<String>();
		acrossHint = new ArrayList<Integer>();
		downHints = new ArrayList<String>();
		downHint = new ArrayList<Integer>();

		mDbHelper = new CrossWordDbAdapter(activity);
		mDbHelper.open();

		if (getAllGame())
			loadDetail();
		else
			mDbHelper.close();

	}

	public void setBackground() {
		try {
			if (pathImg != null && !pathImg.equals("")) {
				Bitmap bitmap = BitmapFactory.decodeFile(pathImg);
				BitmapDrawable background = new BitmapDrawable(bitmap);
				lnMainLayout.setBackgroundDrawable(background);
			}
		} catch (Exception ex) {
			Log.e(tag + "setBackground:", "Error:" + ex.getMessage());
		}
	}

	private boolean getAllGame() {
		Cursor cur = null;
		clsWord clsW;

		try {
			arrListGame = new ArrayList<clsWord>();
			cur = mDbHelper.getGame();

			while (cur.moveToNext()) {
				clsW = new clsWord();
				clsW.setId(cur.getInt(0));
				clsW.setRow(cur.getInt(1));
				clsW.setColumn(cur.getInt(2));
				arrListGame.add(clsW);
			}

			Log.i(tag, "getAllGame() list:" + arrListGame.size());
			if (arrListGame.size() == 0) {
				Log.i(tag, "getAllGame() list:" + arrListGame.size());
				return false;
			}

		} catch (Exception ex) {
			Log.e(tag, "getAllGame() Error: " + ex);
			return false;
		} finally {
			try {
				cur.close();
			} catch (Exception ex) {
			}
		}
		return true;
	}

	private void loadDetail() {
		Log.i(tag, "loadDetail......");
		Cursor cur = null;

		clsWordDetail detail;
		clsWord clsW;
		int rand = -1;
		try {

			// Toast.makeText(activity.getBaseContext(), "Data not found", Toast.LENGTH_SHORT).show();
			for (int r = 0; r < 15; r++) {
				rand = randId(arrListGame.size());
				// tranh truong hop random trung voi cai da co'
				if (rand == MainPlayActivity.randGame) {
					if (rand < arrListGame.size() - 1) {
						rand = arrListGame.size() - 1;
						break;
					}
				}else
					break;
			}
			MainPlayActivity.randGame = rand;
			clsW = (clsWord) arrListGame.get(rand);

			// set row and column for layout game
			MainPlayActivity.row = clsW.getRow();
			MainPlayActivity.column = clsW.getColumn();
			idGame = clsW.getId();
			// get detail game
			cur = mDbHelper.GetDetail(idGame);
			activity.startManagingCursor(cur);
			if (cur != null) {
				Log.i(tag, "get data Hint...");
				while (cur.moveToNext()) {
					detail = new clsWordDetail();
					// hint down
					if (cur.getInt(2) == 0) {
						downHints.add(cur.getInt(1) + ") " + cur.getString(5));
						downHint.add(cur.getInt(1));
						detail.setAcross(false);
					}
					// hint across
					else {
						acrossHints.add(cur.getInt(1) + ") " + cur.getString(5));
						acrossHint.add(cur.getInt(1));
						detail.setAcross(true);
					}
					detail.setNum(cur.getInt(1));
					detail.setPos(cur.getInt(3));
					detail.setWords(cur.getString(4));
					arrList.add(detail);
				}
			}

			Log.i(tag, "loadDetail() ID: " + rand + "; size acrossHints: " + acrossHints.size() + "; list:" + arrList.size());
			// acrossAdapter = new CrosswordHintArrayAdapter(this, acrossHints);
			// downAdapter = new CrosswordHintArrayAdapter(this, downHints);

			// lstAcross.setAdapter(acrossAdapter);
			// lstDown.setAdapter(downAdapter);

		} catch (Exception ex) {
			Log.e(tag, "loadDetail() Error: " + ex);
		} finally {
			try {
				if (cur != null && !cur.isClosed()) {
					cur.close();
				}
				// mDbHelper.close();
			} catch (Exception ex) {
			}
		}
	}

	private int randId(int n) {
		Random randomGenerator = new Random();
		return randomGenerator.nextInt(n);

	}

	public boolean checkWord() {
		int id;
		try {
			Log.i(tag, "checkWord()....");
			// clearGrid();
			for (clsWordDetail wd : arrList) {
				char[] chWords = wd.getWords().toCharArray();
				if (wd.isAcross()) {
					for (int i = 0; i < chWords.length; i++) {
						ExtEditText edt = (ExtEditText) activity.findViewById(wd.getPos() + i);
						if (!edt.getText().toString().toUpperCase().equals(chWords[i] + "")) {
							return false;
						}
					}

				} else {
					for (int i = 0; i < chWords.length; i++) {
						id = wd.getPos() + MainPlayActivity.column * i;
						if (id < MainPlayActivity.row * MainPlayActivity.column) {
							ExtEditText edt = (ExtEditText) activity.findViewById(id);
							if (!edt.getText().toString().toUpperCase().equals(chWords[i] + "")) {
								return false;
							}
						}
					}
				}
			}

			for (clsWord clsW : arrListGame) {
				if (clsW.getId() == idGame) {
					mDbHelper.updateGame(idGame, 1);
					arrListGame.remove(clsW);
					break;
				}
			}
			return true;
		} catch (Exception ex) {
			Log.e(tag + "checkWord", "Error:" + ex.getMessage());
		}
		return false;
	}

	public void CloseDB() {
		if (mDbHelper != null) {
			mDbHelper.close();
			mDbHelper = null;
		}
	}

	// private void setColorItemList(int posCell, boolean isAcross) {
	// try {
	// if (isAcross == true) {
	// for (int i = 0; i < acrossHint.size(); i++) {
	// if (acrossHint.get(i) == posCell) {
	// acrossAdapter.setSelectedPosition(i);
	// downAdapter.setSelectedPosition(-1);
	// // lstAcross.setSelectionFromTop(i, 0);
	// }
	// }
	// } else {
	// for (int i = 0; i < downHint.size(); i++) {
	// if (downHint.get(i) == posCell) {
	// downAdapter.setSelectedPosition(i);
	// acrossAdapter.setSelectedPosition(-1);
	// // lstDown.setSelectionFromTop(i, 0);
	// }
	// }
	// }
	// } catch (Exception ex) {
	// Log.e(tag, "setColorItemList() Error:" + ex.getMessage());
	// }
	// }

}
